#include "SipPlayBackAdaptor.hpp"
#include "SipDialogSet.hpp"
#include "SipUserAgent.hpp"
#include "SipDialog.hpp"

//#include "rutil/Random.hxx"
#include <boost/bind.hpp>

namespace wtsip{


CSipPlayBackAdaptor::CSipPlayBackAdaptor(CSipUserAgent& ua):
CSipDlgAdaptorBase(ua)
{}


CSipPlayBackAdaptor::~CSipPlayBackAdaptor(){}


bool CSipPlayBackAdaptor::sendDevInfo(resip::Data& param){
    if (!m_curDlg){
        CSipDialogSet& dlgSet = m_ua.getDialogSet();
        CSipDialogBase* dlg = dlgSet.createRequestDlg();
        CSipDlgHandle* regThis = dynamic_cast<CSipDlgHandle*>(this);
        dlg->setDlgHandle(regThis);
        m_curDlg = dlg;
    }
    CSipDialogBase* workDlg = m_curDlg;
    const resip::NameAddr& from = m_ua.getParam().getNameAddr();
    if (workDlg){
        m_curParam = param;
        workDlg->sendRequest(m_curTar, from, from, resip::NOTIFY);
    }    
    return true;
}


////////////////////// CSipDlgHandle /////////////////////////
bool CSipPlayBackAdaptor::makeRequest(SipMessage& msg, CSipDialogBase& dlg){
    CSipDialogSet& dlgSet = m_ua.getDialogSet();
    msg.header(h_UserAgent).value() = Data(USER_AGENT);

    CSipUAParameters& uaPm = m_ua.getParam();
    PlainContents contents(m_curParam, uaPm.getMimeType());
    msg.setContents(&contents);

    return true;
}

bool CSipPlayBackAdaptor::makeResponse(SipMessage& msg, CSipDialogBase& dlg){

    msg.header(h_UserAgent).value() = Data(USER_AGENT);
    msg.remove(h_Contacts);
    
    return true;
}

bool CSipPlayBackAdaptor::receiveRqst(SipMessage& msg, CSipDialogBase& dlg){

    CSipDlgAction* action = new CSipDlgAction(dlg.getID(), 
        boost::bind(&CSipDialogBase::sendResponse, &dlg, 200));
    m_ua.getDialogSet().addExecAction(action);
    //msg.getContents()
    //dlg.sendResponse(200);
    
    return true;
}

bool CSipPlayBackAdaptor::receiveResp(SipMessage& msg, CSipDialogBase& dlg){
    return true;
}


}

